//
//  CGSize+AG.swift
//  AGF
//
//  Created by zhuxuhong on 2021/1/21.
//

import UIKit

extension CGSize {
    public static func bw_with(both value: CGFloat) -> Self {
        return .init(width: value, height: value)
    }
    
    public func bw_rescaled(withMaxSize size: CGSize) -> CGSize {
        var w = width
        var h = height
        let ratio = w / h
        let maxW = size.width
        let maxH = size.height
        if w > maxW {
            w = maxW
            h = w / ratio
        }
        if h > maxH {
            h = maxH
            w = h * ratio
        }
        /// 如果上述缩放后还是有任一边超过最大值，继续缩放
        if w > maxW || h > maxH {
            let w = UIScreen.main.bounds.width / 2
            return bw_rescaled(withMaxSize: .init(width: w, height: w))
        }
        return .init(width: w, height: h)
    }
}
